package com.example.tests;

import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.example.utils.SortedListOf;

import static com.example.fw.ContactHelper.CREATION;
import static com.example.tests.ContactDataGenerator.loadContactsFromCsvFile;
import static com.example.tests.ContactDataGenerator.loadContactsFromXmlFile;

public class ContactCreationTests extends TestBase {

	
	@DataProvider
	public  Iterator<Object[]>  contactsFromFile() throws IOException{
		return wrapContactsForDataProvider(loadContactsFromXmlFile(new File("contacts.xml"))).iterator();
	}
	

	@Test (dataProvider = "contactsFromFile")
	  public void testContactCreationWithValidData(ContactData contact) {
		// Старый список контактов
		SortedListOf<ContactData> oldList = app.getModel().getContact();
	    	    
	    // Создание
		app.getContactHelper().createContact(contact, CREATION);
	    
	    // Новый список контактов
		SortedListOf<ContactData> newList = app.getModel().getContact();
	    
	    // Сравнение
		assertThat(newList, equalTo(oldList));
		
		if (timeToCheck()) {
			if ("yes".equals(app.getProperty("check.db"))) {
				assertThat(app.getModel().getContact(), equalTo(app
						.getHibernateHelper().listContacts()));
			}
			if ("yes".equals(app.getProperty("check.ui"))) {
				assertThat(app.getModel().getContact(), equalTo(app
						.getContactHelper().getUiContacts()));
			}
		}
	  }
}
